package com.lgy.controller;

import com.lgy.pojo.Employee;
import com.lgy.service.IEmployeeService;
import com.lgy.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/register/sendMsg.do")
    @ResponseBody
    public ResultData sendMsg(@RequestParam(value = "empPhoneNumber",required = false) String empPhoneNumber ){
        Boolean exist = employeeService.findByPhoneNumber(empPhoneNumber);
        if(exist){
            return ResultData.error("该号码已被注册");
        }
//        PhoneMessageUtil messageUtil = new PhoneMessageUtil();
//        String resultRegister = messageUtil.genRandomNum(4);
//        messageUtil.sandMassage(resultRegister,empPhoneNumber);
        return ResultData.ok(8888);
    }

    @RequestMapping("/register/isRegisterSuccess.do")
    @ResponseBody
    public ResultData register(Employee employee){
        int insert = employeeService.insert(employee);
        if(insert>0){
            return ResultData.ok(true);
        }else  return ResultData.error("注册失败");
    }

    ///employee/isExistEmpName 判断员工姓名
    @RequestMapping("/login/isExistEmpName.do")
    @ResponseBody
    public ResultData isRegisterSuccess(@RequestParam("empname") String empname){
        Boolean exist = employeeService.findByEmpName(empname);
        if(exist){
            return ResultData.ok(true);
        }
        return ResultData.error("该员工不存在");
    }

    ///employee/isExistEmpPhone
    @RequestMapping("/login/isExistEmpPhone.do")
    @ResponseBody
    public ResultData isExistEmpPhone(@RequestParam("companyid") Integer companyid, @RequestParam("empphone") String empphone){
        Boolean exist = employeeService.findEmpByCompanyId(companyid,empphone);
        if(exist){
            return ResultData.ok(true);
        }
        return ResultData.error("该号码不存在");
    }

    //employee/isLoginSuccess
    @RequestMapping("/login/isLoginSuccess.do")
    @ResponseBody
    public ResultData isLoginSuccess(Employee employee){
        Employee emp = employeeService.findEmp(employee);
        if(emp!=null){
            return ResultData.ok(emp);
        }
        return ResultData.error("录入信息有误，请重新输入");
    }
}
